﻿using System;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace Tisbi.LawFirm.BL
{
    public static class MsSqlBackUpHelper
    {
        public static void Backup(string path)
        {
            var sqlConnectionStringBuilder = ConfigurationHelper.GetConnectionStringAsBuilder();
            if (sqlConnectionStringBuilder == null)
            {
                Notifyer.Notify("Не возможно получить настройки для соединения с БД");
                return;
            }

            var bkpDbFullWithCompression = new Backup
            {
                Action = BackupActionType.Database,
                Database = sqlConnectionStringBuilder.InitialCatalog,
                CompressionOption = BackupCompressionOptions.Off
            };

            bkpDbFullWithCompression.Devices.AddDevice(path, DeviceType.File);
            bkpDbFullWithCompression.BackupSetName = "Tisbi Law database Backup - Compressed";
            bkpDbFullWithCompression.BackupSetDescription = "Tisbi Law - Full Backup with Compressin";

            var sqlConnection = new SqlConnection(sqlConnectionStringBuilder.ConnectionString);
            var serverConnection = new ServerConnection(sqlConnection);
            var server = new Server(serverConnection);

            try
            {
                bkpDbFullWithCompression.SqlBackup(server);
                Notifyer.Notify("Процедура создания резервной копии БД прошла успешно.");
            }
            catch (Exception ex)
            {
                Notifyer.NotifyError(ex.ToString(), ex);
            }
        }

        public static void Restore(string path)
        {
            var sqlConnectionStringBuilder = ConfigurationHelper.GetConnectionStringAsBuilder();
            if (sqlConnectionStringBuilder == null)
            {
                Notifyer.Notify("Не возможно получить настройки для соединения с БД");
                return;
            }

            var restoreDb = new Restore
            {
                Database = sqlConnectionStringBuilder.InitialCatalog,
                Action = RestoreActionType.Database
            };

            restoreDb.Devices.AddDevice(path, DeviceType.File);
            restoreDb.ReplaceDatabase = true;

            var sqlConnection = new SqlConnection(sqlConnectionStringBuilder.ConnectionString);
            var serverConnection = new ServerConnection(sqlConnection);
            var server = new Server(serverConnection);

            try
            {
                server.KillAllProcesses(sqlConnectionStringBuilder.InitialCatalog);
                restoreDb.SqlRestore(server);
                DBContextManager.Update();

                Notifyer.Notify("Процедура восстановления из резервной копии БД прошла успешно.");
                System.Windows.Forms.Application.Restart();
                System.Windows.Application.Current.Shutdown();
            }
            catch (Exception ex)
            {
                Notifyer.NotifyError(ex.ToString(), ex);
            }
            
        }
    }
}
